<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 17:03
 */

namespace App\Models\Common;


use App\Models\BaseModel;
use App\Services\Auth\AuthBase;
use Firebase\JWT\JWT;

class MsdTicket extends BaseModel
{

    protected $table = 'msd_ticket';


    public function login($mid,$request)
    {
        $ticket = uuid();
        //插入最后登录时间
        MsdMember::where('member_id',$mid)->update(['login_at'=>date('Y-m-d H:i:s')]);
        $jwt = array(
            'client' => AuthBase::AUTH_USER,
            'iat' => time(),
            'exp' => time() + 3600 * 24,
            'jti' => $ticket
        );
        $jwt = JWT::encode($jwt,env('JWT_SECRET'),'HS256', $mid);
        $add = [
            'uid' => $mid,
            'ticket' => $ticket,
            'token' => $jwt,
            'status' => 0,
            'ip' => $request->ip() ?: '',
            'client_system' => $request->input('client_system', 'admin'),
            'client_version' => env('ClientVersion', '1.0'),
            'api_version' => env('ApiVersion', '1.0'),
            'timeout' => time() + 3600 * 2,
        ];
        $var = $this->create($add)->id;
        // 单点登录，删除其他TOKEN
        // static::where('id', '!=', $var)->where('uid', '=', $uid)->update(['status'=>2]);
        $return = array(
            'ticket' => $ticket,
            'token' => $jwt,
        );
        return $var ? $return : false;
    }

}